<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24"> </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!-- <a-button @click="searchQuery()" type="primary">查询</a-button> -->
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('wms_check')">导出</a-button> -->
      <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel"
      >
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query
        :fieldList="superFieldList"
        ref="superQueryModal"
        @handleSuperQuery="handleSuperQuery"
      ></j-super-query>
      <a-row>
        <a-col :span="6">
          <div style="margin-right: 12px">
            <a-form-model-item prop="warehourse">
              <a-select
                v-model="queryParam.warehourse"
                showSearch
                style="width: 100%; margin-top: 4px"
                placeholder="请选择所属仓库"
                allowClear
              >
                <a-select-option v-for="(item, index) in warehouseOptions" :key="index" :value="item.label">
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </div>
        </a-col>
        <a-button style="margin-top: 4px" type="primary" icon="download" @click="exportWarehouseFile()">
          按仓库导出
        </a-button>
        <a-button style="margin-top: 4px" type="primary" icon="download" @click="exportAllFlies()"> 全量导出 </a-button>
      </a-row>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="state" slot-scope="text">
          {{ text.state * 1 === 0 ? '未完成' : '已完成' }}
        </template>
        <template slot="warehourse" slot-scope="text">
          {{ handleWarehourse(text) }}
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <template slot="amount" slot-scope="text">
          {{ text.amount * 1 }}
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleExport(record)">导出</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>

    <wms-check-modal ref="modalForm" @ok="modalFormOk"></wms-check-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import WmsCheckModal from './modules/WmsCheckModal'
import { mapState } from 'vuex'
import { deleteAction, getAction, downFile, getFileAccessHttpUrl } from '@/api/manage'
import { stateObjToArray } from '@/utils/common.js'
export default {
  name: 'WmsCheckList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    WmsCheckModal,
  },
  data() {
    return {
      description: 'wms_check管理页面',
      relWarehouseIds: '',
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '盘点编号',
          align: 'center',
          dataIndex: 'checkNo',
        },
        {
          title: '盘点数量',
          align: 'center',
          scopedSlots: { customRender: 'amount' },
        },
        {
          title: '创建人',
          align: 'center',
          dataIndex: 'createBy',
        },
        // {
        //   title: '组织',
        //   align: 'center',
        //   dataIndex: 'organization',
        // },
        {
          title: '状态',
          align: 'center',
          // dataIndex: 'state',
          scopedSlots: { customRender: 'state' },
        },
        {
          title: '所属仓库',
          align: 'center',
          scopedSlots: { customRender: 'warehourse' },
          // dataIndex: 'warehourse',
        },
        {
          title: '创建时间',
          align: 'center',
          dataIndex: 'createTime',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/wms/wmsCheck/list',
        delete: '/wms/wmsCheck/delete',
        deleteBatch: '/wms/wmsCheck/deleteBatch',
        exportXlsUrl: 'wms/wmsCheck/exportXls',
        importExcelUrl: 'wms/wmsCheckDetail/importExcel',
        exportAllFilesUrl: 'wms/wmsCheckMain/exportXls',
        exportFilesUrl: 'wms/wmsCheckMain/exportWareInfo',
      },
      dictOptions: {},
      superFieldList: [],
    }
  },
  created() {
    this.getSuperFieldList()
    this.$store.commit('getEnableWarehouse')
  },
  computed: {
    ...mapState({
      enableWarehouse: (s) => s.dictionary.enableWarehouse,
      warehouseOptions: (s) => {
        return stateObjToArray(s.dictionary.enableWarehouse)
      },
    }),
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    exportWarehouseFile() {
      if (!this.queryParam.warehourse && this.queryParam.warehourse != '') {
        return this.$message.warning('请选择仓库')
      }
      try {
        let that = this
        let param = this.getQueryParams()
        if (this.selectedRowKeys && this.selectedRowKeys.length > 0) {
          param['selections'] = this.selectedRowKeys.join(',')
        }
        console.log('导出参数', param)
        downFile(this.url.exportFilesUrl, {
          warehourse: that.queryParam.warehourse,
        }).then((data) => {
          if (!data) {
            this.$message.warning('文件下载失败')
            return
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(
              new Blob([data], { type: 'application/vnd.ms-excel' }),
              that.relWarehouseIds + '.xls'
            )
          } else {
            let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', that.relWarehouseIds + '.xls')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link) //下载完成移除元素
            window.URL.revokeObjectURL(url) //释放掉blob对象
          }
        })
      } catch (err) {
        console.log(err)
      }
    },
    exportAllFlies() {
      try {
        let param = this.getQueryParams()
        if (this.selectedRowKeys && this.selectedRowKeys.length > 0) {
          param['selections'] = this.selectedRowKeys.join(',')
        }
        console.log('导出参数', param)
        downFile(this.url.exportAllFilesUrl, {}).then((data) => {
          if (!data) {
            this.$message.warning('文件下载失败')
            return
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), '全部盘点任务.xls')
          } else {
            let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', '全部盘点任务' + '.xls')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link) //下载完成移除元素
            window.URL.revokeObjectURL(url) //释放掉blob对象
          }
        })
      } catch (err) {
        console.log(err)
      }
    },
    handleExport(e) {
      // console.log('handleExport', e)
      this.handleExportXls('wms_check_' + e.checkNo, { id: e.id, field: '' })
    },
    handleWarehourse(e) {
      const row = this.enableWarehouse[e.warehourse]
      if (row) {
        return row.text
      } else {
        return e.warehourse
      }
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'checkNo', text: '盘点编号' })
      fieldList.push({ type: 'string', value: 'amount', text: '盘点数量' })
      fieldList.push({ type: 'string', value: 'createBy', text: '创建人' })
      fieldList.push({ type: 'string', value: 'organization', text: '组织' })
      fieldList.push({ type: 'string', value: 'state', text: '状态' })
      fieldList.push({ type: 'string', value: 'company', text: '所属公司' })
      fieldList.push({ type: 'string', value: 'wareName', text: '所属仓库' })
      this.superFieldList = fieldList
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
